//所有接口
//axios会把参数对象转换成json格式传给后台
//axios会在headers中添加Content-Type=application/json
import axios from "../utils/request.js";
import { getStorage } from "@/utils/storage";

//频道-获取所有频道
export const getAllChannelsAPI = () => {
  return axios({ url: "/v1_0/channels" });
};

//频道-获取用户频道
// 有用户返回用户频道，没有用户则返回默认频道
export const getUserChannelsAPI = () => {
  return axios({
    url: "/v1_0/user/channels",
  });
};

//频道-更新用户频道数据（覆盖式）
export const updateChannelsAPI = (channels) => {
  return axios({
    url: "/v1_0/user/channels",
    method: "PUT",
    data: { channels },
  });
};

//频道-删除指定频道
export const delChannelAPI = (target) => {
  return axios({ url: `/v1_0/user/channels/${target}`, method: "DELETE" });
};

//登录-用户登录
export const loginAPI = (user) => {
  return axios({ url: "/v1_0/authorizations", method: "POST", data: user });
};

// 用户 - 更新token
export const refreshTokenAPI = () =>
  axios({
    url: "/v1_0/authorizations",
    method: "PUT",
    headers: {
      Authorization: `Bearer ${getStorage("refresh_token")}`,
    },
  });

//用户-关注用户
export const followedAPI = (userid) => {
  return axios({
    url: "/v1_0/user/followings",
    method: "POST",
    data: { target: userid },
  });
};

//用户-取消关注用户
export const unFollowedAPI = (userid) => {
  return axios({ url: `/v1_0/user/followings/${userid}`, method: "DELETE" });
};

// 用户 - 基本资料
export const userInfoAPI = () => {
  return axios({
    url: "/v1_0/user",
  });
};

// 用户- 个人资料(就为了获取生日)
export const userProfileAPI = () => {
  return axios({
    url: "/v1_0/user/profile",
  });
};

// 用户- 更新头像
// 注意: formObj的值必须是一个表单对象
// '{"a": 10, "b": 20}' // 对象格式的JSON字符串
// new FormData() // 表单对象
export const updatePhotoAPI = (formObj) => {
  return axios({
    url: "/v1_0/user/photo",
    method: "PATCH",
    data: formObj,
    // 如果你的请求体内容是表单对象, 浏览器会自动携带请求头Content-Type为multipart/form-data
  });
};

// 用户 - 更新资料
export const updateProfileAPI = ({ birthday, userName }) => {
  return axios({
    url: "/v1_0/user/profile",
    method: "PATCH",
    data: {
      birthday: birthday,
      name: userName,
    },
  });
};

//新闻-获取所有新闻
export const getAllArticlesAPI = ({ channel_id, timestamp }) => {
  return axios({
    url: "/v1_0/articles",
    params: {
      channel_id,
      timestamp,
    },
  });
};

//文章-反馈-不感兴趣
export const dislikeArticleAPI = (artId) => {
  return axios({
    url: "/v1_0/article/dislikes",
    method: "POST",
    data: {
      target: artId,
    },
  });
};

//文章-反馈-举报内容
export const reportArticleAPI = ({ artId, type }) => {
  return axios({
    url: "/v1_0/article/reports",
    method: "POST",
    data: {
      target: artId,
      type: type,
    },
  });
};

//文章-获取详情
export const detailAPI = (id) => {
  return axios({ url: `/v1_0/articles/${id}` });
};

//文章-点赞
export const loveArticleAPI = (artId) => {
  return axios({
    url: "/v1_0/article/likings",
    method: "POST",
    data: {
      target: artId,
    },
  });
};

//文章-取消点赞
export const unLoveArticleAPI = (artId) => {
  return axios({ url: `/v1_0/article/likings/${artId}`, method: "DELETE" });
};

//文章-获取评论
export const commentsListAPI = ({ artId, offset = null, limit = 10 }) => {
  return axios({
    //axios针对键值对为空的params参数，默认不加在网址后面
    url: `/v1_0/comments`,
    method: "GET",
    params: {
      type: "a",
      source: artId,
      offset,
      limit,
    },
  });
};

// 评论 - 喜欢
export const commentLikingAPI = (comId) => {
  return axios({
    url: "/v1_0/comment/likings",
    method: "POST",
    data: {
      target: comId,
    },
  });
};
// 评论-取消喜欢
export const commentDisLikingAPI = (comId) => {
  return axios({
    url: `/v1_0/comment/likings/${comId}`,
    method: "DELETE",
  });
};

// 评论 - 发布
export const sendCommentAPI = ({ id, content, art_id = null }) => {
  let obj = { target: id, content: content };
  if (art_id) {
    obj.art_id = art_id;
  }
  return axios({
    url: "/v1_0/comments",
    method: "POST",
    data: obj,
  });
};
//搜索-联想列表
export const suggestListAPI = (kw) => {
  return axios({ url: "/v1_0/suggestion", method: "GET", params: { q: kw } });
};

//搜索-搜索结果
export const searchResultsAPI = ({ page = 1, per_page = 10, q }) => {
  return axios({
    url: "/v1_0/search",
    method: "GET",
    params: { page, per_page, q },
  });
};
